package com.dqx.haoqiu.mvp.module.launch;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import com.dqx.haoqiu.mvp.R;
import com.dqx.haoqiu.mvp.common.base.BaseActivity;
import com.dqx.haoqiu.mvp.module.launch.dagger.DaggerLaunchComponent;
import com.dqx.haoqiu.mvp.module.launch.dagger.LaunchModule;
import com.dqx.haoqiu.mvp.utils.Navigation;
import com.dqx.haoqiu.mvp.widget.PrivacyDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.dqx.haoqiu.mvp.common.base.BaseApplication.getAppComponent;

/**
 * 启动页
 */
public class LaunchActivity extends BaseActivity<LaunchPresenter> implements LaunchContract.View {

    @BindView(R.id.tv_time)
    TextView tvTime;

    //倒计时时长
    int duration = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        ButterKnife.bind(this);

        //显示倒计时数字3
        tvTime.setText(getString(R.string.pass_time, duration));

        //请求权限
        requestPermission(new String[]{Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
        }, this::showPrivacyDialog, getString(R.string.required_permissions));
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        DaggerLaunchComponent.builder()
                .appComponent(getAppComponent())
                .launchModule(new LaunchModule(this))
                .build().inject(this);
    }

    /**
     * 显示隐私政策弹窗
     */
    private void showPrivacyDialog() {
        if (presenter.showPrivaceDialog()) {//显示隐私弹窗
            new PrivacyDialog(this)
                    .setConfirmListener(dialog -> {
                        //执行倒计时任务
                        presenter.countdown(duration, tvTime);
                        //设置不再显示隐私政策弹窗的标记
                        presenter.setShowPrivaceDialog(false);
                    }).show();
        } else {//不显示隐私弹窗
            presenter.countdown(duration, tvTime);
        }
    }

    /**
     * 倒计时结束
     */
    @Override
    public void countdownFinish() {
        if (presenter.showGuide()) {//显示引导页
            Navigation.openGuide(this);
        } else {//不显示引导页
            Navigation.openLogin(this);
        }
        finish();
    }

    /**
     * 不支持侧滑退出
     *
     * @return
     */
    @Override
    public boolean supportSlideBack() {
        return false;
    }

    /**
     * 支持沉浸式
     *
     * @return
     */
    @Override
    public boolean supporTimmersion() {
        return true;
    }

    /**
     * 设置状态栏背景色为透明
     *
     * @return
     */
    @Override
    public int statusBarResource() {
        return R.color.transparent;
    }

    /**
     * 隐藏标题栏
     *
     * @return
     */
    @Override
    public boolean setTitleBarVisable() {
        return false;
    }
}
